<script>
import {defineComponent} from 'vue'
import {handleError, handleSuccess} from "@/util/request";

export default defineComponent({
  name: "MaterialSavePage",

  data() {
    return {

      item: {},
      rules: {
        materialName: [
          {required: true, message: "请输入物资名称", trigger: 'blur'},
        ],

        stockAmount: [
          // {required:true,message:"",trigger:'blur'}
        ]
      },
      action:process.env.VUE_APP_API_BASE+'material/batch-import'
    }
  },

  methods: {

    handleSubmit(formName) {

      this.$refs[formName].validate((valid) => {
            if (valid) {
              this.$http.post('/material/save', this.item)
                  .then((result) => {
                    handleSuccess(result)
                  })

                  .catch((error) => {
                    handleError(error)
                  })
            }
          }
      )
    },

    handleReset(formName) {
      this.$refs[formName].resetFields();
    },

    onSuccess(result){
      handleSuccess({data:result})
    },

    onError(error){
      handleError(error)
    }

  }


})
</script>

<template>
  <el-card class="box-card">
    <div slot="header" class="clearfix">
      <span>添加物资</span>
      <el-upload :action="action"
                 accept=".xlsx,xls"
                 show-file-list="false"
                 :on-success="onSuccess"
                 :on-error="onError"
                 with-credentials
                 style="float: right">
      <el-button style="padding: 3px 0" type="text" >批量导入Excel</el-button>
      </el-upload>
    </div>
    <el-form label-width="100px" ref="myForm" :model="item" :rules="rules">
      <el-form-item label="物资名称" prop="materialName">
        <el-input v-model="item.materialName" placeholder="请输入物资名称"/>
      </el-form-item>
      <el-form-item label="库存数量" prop="stockAmount">
        <el-input v-model="item.stockAmount" placeholder="请输入物资库存"/>
      </el-form-item>
      <el-form-item label="库存预警" prop="warningStock">
        <el-input v-model="item.warningStock" placeholder="请输入库存预警"/>
      </el-form-item>
      <el-form-item>
        <el-button @click="handleSubmit('myForm')" type="primary">提交</el-button>
        <el-button @click="handleReset('myForm')">重置</el-button>
      </el-form-item>
    </el-form>
  </el-card>




</template>

<style scoped>

</style>